package com.tangyuan.answer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangyuan.answer.mapper.WrongGradeMapper;
import com.tangyuan.answer.service.IAnswerCollentService;
import com.tangyuan.answer.service.IAnswerObjectGradeService;
import com.tangyuan.answer.service.IAnswertopaperService;
import com.tangyuan.answer.service.IWrongGradeService;
import com.tangyuan.common.result.Result;
import com.tangyuan.common.result.ResultCodeEnum;
import com.tangyuan.dto.RemoveGradeDto;
import com.tangyuan.model.AnswerCollent;
import com.tangyuan.model.AnswerObjectGrade;
import com.tangyuan.model.Answertopaper;
import com.tangyuan.model.WrongGrade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class WrongGradeServiceImpl extends ServiceImpl<WrongGradeMapper, WrongGrade> implements IWrongGradeService {

    @Autowired
    private WrongGradeMapper wrongGradeMapper;
    @Autowired
    private IAnswerCollentService answerCollentService;

    @Autowired
    private IAnswerObjectGradeService answerObjectGradeService;

    @Autowired
    private IAnswertopaperService answertopaperService;

    @Override
    public Result getAnswerGradeByAnswerObjectId(Long userId, Long answerObjectId) {
        if (userId == null && answerObjectId == null) {
            return Result.build(null, ResultCodeEnum.ARGUMENT_VALID_ERROR);
        }
        return Result.ok(wrongGradeMapper.getAnswerGradeByAnswerObjectId(userId, answerObjectId));
    }

    @Override
    public Result getWrongGradeList(Long userId) {
        List<WrongGrade> wrongGradeList = wrongGradeMapper.selectList(null);
        return Result.ok(wrongGradeList);
    }
    @Transactional(rollbackFor=Exception.class)
    @Override
    public Result removeWrongGrade(Long userId, RemoveGradeDto removeGradeDto) {
        if (userId == null && removeGradeDto == null) {
            return Result.build(null, ResultCodeEnum.ARGUMENT_VALID_ERROR);
        }
        for (Long gradeId : removeGradeDto.getGradeIdList()) {
            LambdaQueryWrapper<WrongGrade> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WrongGrade::getUserId, userId);
            lambdaQueryWrapper.eq(WrongGrade::getId, gradeId);
            WrongGrade wrongGrade = wrongGradeMapper.selectOne(lambdaQueryWrapper);
            if (wrongGrade == null) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return Result.build("请选择正确的年级", ResultCodeEnum.NO_DATA);
            }
            LambdaQueryWrapper<AnswerObjectGrade> checkQueryWrapper = new LambdaQueryWrapper<>();
            checkQueryWrapper.eq(AnswerObjectGrade::getAnswerObjectId, removeGradeDto.getAnswerObjectId());
            checkQueryWrapper.eq(AnswerObjectGrade::getGradeId, gradeId);
            AnswerObjectGrade checkOne = answerObjectGradeService.getOne(checkQueryWrapper);
            if (checkOne == null){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return Result.build("请选择正确的科目年级", ResultCodeEnum.NO_DATA);
            }
            if (!userId.equals(wrongGrade.getUserId())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return Result.build(null, ResultCodeEnum.YOU_HAVE_NO_CHANGE);
            }

            LambdaQueryWrapper<AnswerCollent> collentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            collentLambdaQueryWrapper.eq(AnswerCollent::getUserId, userId);
            collentLambdaQueryWrapper.eq(AnswerCollent::getAnswerObjectId, removeGradeDto.getAnswerObjectId());
            collentLambdaQueryWrapper.eq(AnswerCollent::getIsDelete, 0);
            collentLambdaQueryWrapper.eq(AnswerCollent::getGradeId, gradeId);
            List<AnswerCollent> answerCollentList = answerCollentService.list(collentLambdaQueryWrapper);

            if (answerCollentList.size() != 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return Result.build("你选择的科目存在错题,请删除完错题之后重试", ResultCodeEnum.ANSWER_IS_NOT_NULL);
            }

            LambdaQueryWrapper<Answertopaper> toPaperLambdaQueryWrapper = new LambdaQueryWrapper<>();
            toPaperLambdaQueryWrapper.eq(Answertopaper::getUserId, userId);
            toPaperLambdaQueryWrapper.eq(Answertopaper::getObjectId, removeGradeDto.getAnswerObjectId());
            toPaperLambdaQueryWrapper.eq(Answertopaper::getGradeId, gradeId);
            toPaperLambdaQueryWrapper.eq(Answertopaper::getIsDelete, 0);
            List<Answertopaper> answerToPaperList = answertopaperService.list(toPaperLambdaQueryWrapper);
            if (answerToPaperList.size() != 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return Result.build("你选择的科目存在错题试卷,请删除完错题之后重试", ResultCodeEnum.ANSWER_IS_NOT_NULL);
            }

            LambdaQueryWrapper<AnswerObjectGrade> answerOGradeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            answerOGradeLambdaQueryWrapper.eq(AnswerObjectGrade::getAnswerObjectId, removeGradeDto.getAnswerObjectId());
            answerOGradeLambdaQueryWrapper.eq(AnswerObjectGrade::getGradeId, gradeId);
            answerObjectGradeService.remove(answerOGradeLambdaQueryWrapper);
        }
        return Result.ok();
    }

    @Override
    public Result getPaperGradeByAnswerObjectId(Long userId, Long answerObjectId) {
        if (userId == null && answerObjectId == null) {
            return Result.build(null, ResultCodeEnum.ARGUMENT_VALID_ERROR);
        }
        return Result.ok(wrongGradeMapper.getPaperGradeByAnswerObjectId(userId, answerObjectId));
    }
}
